﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;

namespace Ocean.His.MedicalInsurance
{
    public class MedicalInsuranceShenYangProvider : MedicalInsuranceBaseProvider, ISingletonDependency
    {
        private ILogger _logger;
        private MedicalInsuranceOption _medicalInsuranceOption;
        private IGuidGenerator _guidGenerator;

        #region Dll

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="pInitInfo"></param>
        /// <param name="pErrMsg"></param>
        /// <returns></returns>
        [DllImport("HeaSecReadInfo.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        protected static extern int Init(string pInitInfo, StringBuilder pErrMsg);

        /// <summary>
        /// 读卡
        /// </summary>
        /// <param name="pCardInfo"></param>
        /// <param name="pBusiCardInfo"></param>
        /// <returns></returns>
        [DllImport("HeaSecReadInfo.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        protected static extern int ReadCardBas(StringBuilder pCardInfo, StringBuilder pBusiCardInfo);

        /// <summary>
        /// 电子社保卡解码
        /// </summary>
        /// <param name="pOutBuff"></param>
        /// <returns></returns>
        [DllImport("HeaSecReadInfo.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        protected static extern int EcCardQuery(StringBuilder pOutBuff);

        /// <summary>
        /// 电子凭证解码
        /// </summary>
        /// <param name="pOutBuff"></param>
        /// <returns></returns>
        [DllImport("HeaSecReadInfo.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        protected static extern int EcQuery(string pInData, StringBuilder pOutBuff);

        /// <summary>
        /// 修改PIN码
        /// </summary>
        /// <param name="pOutBuff"></param>
        /// <returns></returns>
        [DllImport("HeaSecReadInfo.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        protected static extern int ChangePIN(StringBuilder pOutBuff);

        #endregion

        public MedicalInsuranceShenYangProvider(ILogger logger
            , MedicalInsuranceOption medicalInsuranceOption
            , IGuidGenerator guidGenerator) : base(logger, medicalInsuranceOption, guidGenerator)
        {
            _logger = logger;
            _medicalInsuranceOption = medicalInsuranceOption;
            _guidGenerator = guidGenerator;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="siInitModel"></param>
        /// <returns></returns>
        public override  Tuple<int, string> Init(InitRequestModel initRequestModel)
        {
            try
            {
                StringBuilder pErrMsgSB = new StringBuilder(20480);
                var pInitInfo = JsonConvert.SerializeObject(initRequestModel);
                _logger.LogInformation("初始化动态库：开始");
                _logger.LogInformation(pInitInfo);

                var resultCode = Init(pInitInfo, pErrMsgSB);
                var resultMsg = pErrMsgSB.ToString();
                var result = new Tuple<int, string>(resultCode, resultMsg);

                _logger.LogInformation(result.Item1.ToString());
                _logger.LogInformation(result.Item2);

                return result;
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                _logger.LogError(e.StackTrace);
                return new Tuple<int, string>(-1, e.Message.ToString());
            }
        }

        /// <summary>
        /// 修改PIN码
        /// </summary>
        /// <returns></returns>
        public override Tuple<int, string> ChangePIN()
        {
            try
            {
                _logger.LogInformation(MedicalInsuranceConst.CHANGE_PIN_START);
                StringBuilder pOutBuffSB = new StringBuilder(2048);
                var resultCode = ChangePIN(pOutBuffSB);

                if (resultCode == 0)
                {
                    _logger.LogInformation(pOutBuffSB.ToString());
                    return new Tuple<int, string>(0, pOutBuffSB.ToString());
                }
                else
                {
                    _logger.LogError(pOutBuffSB.ToString());
                    return new Tuple<int, string>(-1, pOutBuffSB.ToString());
                }
            }
            catch (Exception e)
            {
                return new Tuple<int, string>(-1, e.Message);
            }
        }
    }
}
